package action.newelifile;

import java.io.File;
import java.util.ArrayList;
import java.util.Locale;

import org.apache.commons.net.ftp.FTPClient;
import action.adapter.CopyLocalPopupAdapter;
import action.pojo.LocalFilePojo;
import action.util.LoadSetting;
import action.util.Tool;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Video.Thumbnails;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CopyLocalPopup extends Activity {

	TextView textView,txtAddressLocal,txtViewEmpty;
	ListView listLocalDetailPopup;
	ImageView imgBackButton;
	ArrayList<LocalFilePojo> list_local_popup;
	File[] listfileLocal;
	CopyLocalPopupAdapter ftpPopupAdapter;
	Button pasteftp, cancelftp;
	FTPClient gclient=new FTPClient();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initLocale();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.copyftp_popup);
		getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		initAttribute();
		textView.setText(Tool.getLocalPath(Tool.dest_ftp_path_popup));
		loadlist();
		pasteftp.setOnClickListener(processPaste());
		listLocalDetailPopup.setOnItemClickListener(listClickDetail());
	}
	
	private OnClickListener processPaste(){
		OnClickListener onclick = new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Tool.dest_ftp_path=Tool.getLocalPath(Tool.dest_ftp_path_popup);
				System.out.println("nguon: "+Tool.src_ftp_path+" ---- dich: "+Tool.dest_ftp_path);
				File destf = new File(Tool.dest_ftp_path);
				File[] fs = destf.listFiles();
				if(fs!=null && fs.length>0){
					for(int i=0;i<Tool.multiSelect.size();i++){
						boolean kttrung = false;
						for(int j=0;j<fs.length;j++){
							if(fs[i].getName().equals(Tool.multiSelect.get(i).getFtpFile().getName())){
								kttrung = true;
							}
						}
						if(kttrung==true){
							System.out.println("xu ly trung");
						}else{
							System.out.println("copy doi tuong");
						}
					}
				}else{
					System.out.println("copy het");
				}
			}
		};
		return onclick;
	}
	
	public void initLocale(){
		String languageToLoad = Tool.locale(LoadSetting.setting.getLanguage());  
		Locale locale = new Locale(languageToLoad);   
		Locale.setDefault(locale);  
		Configuration config = new Configuration();  
		config.locale = locale;  
		getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());  
	}
	
	public void initAttribute(){
		textView = (TextView)findViewById(R.id.tvHeaderFtp);
		txtAddressLocal = (TextView)findViewById(R.id.txtAddressFTP);
		txtViewEmpty = (TextView)findViewById(R.id.txtViewEmpty);
		listLocalDetailPopup = (ListView)findViewById(R.id.listFtpDetailPopup);
		pasteftp = (Button)findViewById(R.id.btPasteFtp);
		cancelftp = (Button)findViewById(R.id.btCancelFtp);
		imgBackButton = (ImageView)findViewById(R.id.imgBackButton);
		
		pasteftp.setText(getString(R.string.paste));
		cancelftp.setText(getString(R.string.cancel));
		
		cancelftp.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Tool.dest_ftp_path="";
				Tool.dest_ftp_path_popup.clear();
				setResult(0);
				finish();
			}
		});
		
		imgBackButton.setOnClickListener(imiBackClick());
	}
	
	private OnClickListener imiBackClick(){
		OnClickListener onclick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(Tool.dest_ftp_path_popup.size()>=2){
					Tool.dest_ftp_path_popup.remove(Tool.dest_ftp_path_popup.size()-1);
					loadlist();
				}
				
			}
		};
		return onclick;
	}
	
	private ArrayList<LocalFilePojo> getListFile(String path) {
		ArrayList<LocalFilePojo> all = new ArrayList<LocalFilePojo>();
		ArrayList<LocalFilePojo> files = new ArrayList<LocalFilePojo>();
		ArrayList<LocalFilePojo> folders = new ArrayList<LocalFilePojo>();
		File dir = new File(path);
		Intent action = null;
		Bitmap bitmap = null;
		Drawable icon = null;
		LocalFilePojo item;
		BitmapFactory.Options options=new BitmapFactory.Options();
		options.inSampleSize = 8;
		try {
			for (File f : dir.listFiles()) {
				if (f.canRead()) {
					if (f.isFile()) {
						action = new Intent(Intent.ACTION_VIEW);
						if (Tool.checkExtendFile(f.getName(), ".txt")
								|| Tool.checkExtendFile(f.getName(), ".csv")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.text_file);
							action.setDataAndType(Uri.fromFile(f), "text/*");
						} else if (Tool.checkExtendFile(f.getName(), ".xml")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.xml_file);
							action.setDataAndType(Uri.fromFile(f), "text/*");
						} else if (Tool.checkExtendFile(f.getName(), ".flv")
								|| Tool.checkExtendFile(f.getName(), ".3gp")
								|| Tool.checkExtendFile(f.getName(), ".avi")) {
							bitmap = ThumbnailUtils.createVideoThumbnail(
									f.getAbsolutePath(), Thumbnails.MICRO_KIND);
							icon = new BitmapDrawable(bitmap);
							action.setDataAndType(Uri.fromFile(f), "video/*");
						} else if (Tool.checkExtendFile(f.getName(), ".mp3")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.mp3_file);
							action.setDataAndType(Uri.fromFile(f), "audio/*");
						} else if (Tool.checkExtendFile(f.getName(), ".doc")
								|| Tool.checkExtendFile(f.getName(), ".docx")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.word_file);
							action.setDataAndType(Uri.fromFile(f), "text/*");
						} else if (Tool.checkExtendFile(f.getName(), ".ppt")
								|| Tool.checkExtendFile(f.getName(), ".pptx")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.pptx_file);
							action.setDataAndType(Uri.fromFile(f), "text/*");
						} else if (Tool.checkExtendFile(f.getName(), ".xls")
								|| Tool.checkExtendFile(f.getName(), ".xlsx")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.xlsx_file);
							action.setDataAndType(Uri.fromFile(f), "text/*");
						} else if (Tool.checkExtendFile(f.getName(), ".zip")
								|| Tool.checkExtendFile(f.getName(), ".rar")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.rar_file);
							action.setDataAndType(Uri.fromFile(f), "video/*");
						} else if (Tool.checkExtendFile(f.getName(), ".jpg")
								|| Tool.checkExtendFile(f.getName(), ".jpeg")
								|| Tool.checkExtendFile(f.getName(), ".png")
								|| Tool.checkExtendFile(f.getName(), ".bmp")
								|| Tool.checkExtendFile(f.getName(), ".gif")) {
							bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),options);
							icon = new BitmapDrawable(bitmap);
							action.setDataAndType(Uri.fromFile(f), "image/*");
						} else if (Tool.checkExtendFile(f.getName(), ".apk")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.apk_file);
							action.setDataAndType(Uri.fromFile(f),
							"application/vnd.android.package-archive");
						} else if (Tool.checkExtendFile(f.getName(), ".exe")) {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.exe_file);
						} else {
							icon = CopyLocalPopup.this.getResources().getDrawable(
									R.drawable.unknown_file);
						}
						item = new LocalFilePojo(f,icon,action);
						files.add(item);
					} else if (f.isDirectory()) {
						icon = LoadSetting.loadFolder(CopyLocalPopup.this);
						item = new LocalFilePojo(f, icon);
						folders.add(item);
					}
				}
			}

			Tool.sortLocal(files, 0);
			Tool.sortLocal(folders, 0);

			all.addAll(folders);
			all.addAll(files);

		} catch (NullPointerException e) {
			e.printStackTrace();
			return null;
		}
		return all;
	}
	
	private void loadlist(){
		String pth=Tool.getLocalPath(Tool.dest_ftp_path_popup);
		list_local_popup = getListFile(pth);
		ftpPopupAdapter = new CopyLocalPopupAdapter(CopyLocalPopup.this, R.layout.list_view, list_local_popup);
		listLocalDetailPopup.setAdapter(ftpPopupAdapter);
		txtAddressLocal.setText(pth);
	}
	
	private OnItemClickListener listClickDetail(){
		OnItemClickListener onclick = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				LocalFilePojo item =(LocalFilePojo) arg0.getItemAtPosition(arg2);
				if(item.getFileItem().isDirectory()){
					Tool.dest_ftp_path_popup.add(item.getFileItem().getName());
					loadlist();
				}else{
					listLocalDetailPopup.setOnItemClickListener(null);
				}
			}
		};
		return onclick;
	}
}
